#include "propertywidget.h"
#include "ui_propertywidget.h"
#include <QDebug>
#include <QMessageBox>

PropertyWidget::PropertyWidget(const PropertyCollection &properties_, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PropertyWidget)
{
    ui->setupUi(this);

    this->properties = &properties_;

    // Set up connection between data changes and UI

    connect(this, SIGNAL(addPropertyRequested(GameProperty,QString)),
            properties, SLOT(add(GameProperty,QString)));
    connect(this, SIGNAL(updatePropertyRequested(QString,GameProperty,QString)),
            properties, SLOT(update(QString,GameProperty,QString)));
    connect(this, SIGNAL(removePropertyRequested(QString)),
            properties, SLOT(remove(QString)));

    connect(properties, SIGNAL(propertyAdded(GameProperty,QString)),
            this, SLOT(addProperty(GameProperty,QString)));
    connect(properties, SIGNAL(propertyUpdated(QString,GameProperty,QString)),
            this, SLOT(updateProperty(QString,GameProperty,QString)));
    connect(properties, SIGNAL(propertyRemoved(QString)),
            this, SLOT(removeProperty(QString)));

    connect(properties, SIGNAL(cleared()), this, SLOT(clearAll()));
}

PropertyWidget::~PropertyWidget()
{
    delete ui;
}

void PropertyWidget::clearPropertyForm() {
    ui->propName->setText("");
    ui->propDefaultValue->setText("");
    ui->propKey->setText("");
}

QList<KeyPair<GameProperty> > PropertyWidget::getOrderedProperties() {
    QList<KeyPair<GameProperty> > list;
    for(int i = 0; i<ui->propList->count(); ++i) {
        QString key = ui->propList->item(i)->data(Qt::UserRole).toString();
        list.append(KeyPair<GameProperty>(key, properties->get(key)));
    }
    return list;
}

void PropertyWidget::on_propList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if(current) {
        QString key = current->data(Qt::UserRole).toString();
        if(properties->contains(key)) {
            GameProperty property = properties->get(key);
            ui->propName->setText(property.getName());
            ui->propDefaultValue->setText(property.defaultValue);
            ui->propKey->setText(key);
            ui->propDelete->setEnabled(true);
            ui->propUpdate->setEnabled(true);
        } else {
            qDebug() << "The ID does not exist in properties";
            ui->propDelete->setEnabled(false);
            ui->propUpdate->setEnabled(false);
            clearPropertyForm();
        }
    } else {
        clearPropertyForm();
        ui->propDelete->setEnabled(false);
        ui->propUpdate->setEnabled(false);
    }

}

void PropertyWidget::on_propAdd_clicked()
{
    GameProperty property(ui->propName->text(),ui->propDefaultValue->text());
    emit addPropertyRequested(property, ui->propKey->text());
}

void PropertyWidget::on_propUpdate_clicked()
{
    GameProperty property(ui->propName->text(),ui->propDefaultValue->text());
    QString oldKey = ui->propList->currentItem()->data(Qt::UserRole).toString();
    emit updatePropertyRequested(oldKey, property, ui->propKey->text());
}

void PropertyWidget::on_propDelete_clicked()
{
    if(QMessageBox::warning(0, tr("Are you sure?"), tr("Deleting a property will make invalid all values that use it. Are you sure you want to continue?"),
                            QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
        emit removePropertyRequested(ui->propList->currentItem()->data(Qt::UserRole).toString());

}

void PropertyWidget::addProperty(const GameProperty &property, const QString &key) {
    Q_ASSERT_X(!keyIndex.contains(key), "PropertyWidget::addProperty()", "Attempting to replace existing element");
    QListWidgetItem* item = new QListWidgetItem(property.getName());
    item->setData(Qt::UserRole, QVariant(key));
    ui->propList->addItem(item);
    keyIndex.insert(key, item);
    clearPropertyForm();
}

void PropertyWidget::updateProperty(const QString &oldKey, const GameProperty &newProperty,
                                    const QString &newKey) {
    QListWidgetItem* item = keyIndex.value(oldKey, NULL);
    if(item) {
        if(oldKey != newKey) {
            item->setData(Qt::UserRole, QVariant(newKey));
            keyIndex.remove(oldKey);
            keyIndex.insert(newKey, item);
        }
        item->setText(newProperty.getName());
    }
}

void PropertyWidget::removeProperty(const QString &key) {
    QListWidgetItem *item = keyIndex.value(key, NULL);
    if(item) {
        delete item;
        keyIndex.remove(key);
    }
}

void PropertyWidget::clearAll() {
    ui->propList->clear();
    keyIndex.clear();
    clearPropertyForm();
}

void PropertyWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}
